﻿using Ezpay.UI.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ezpay.UI
{
    static class GlobalDB
    {
        static GlobalDB()
        {
            HousingAgencies = new List<string>() { "Okinawa Apartments", "Okinawa Highrise" };
            Customers = new List<Customer>() { BuildCustomer("Terry", "Tsay", 0), BuildCustomer("Mike", "Shaw", 1) };

        }
        public static List<Customer> Customers { get; set; }
        public static List<string> HousingAgencies { get; set; }

        public static void AddCustomer(Customer customer)
        {
            GenerateOtherStuff(customer);
            Customers.Add(customer);
        }
        public static Customer GetCustomer(string customerId)
        {
            return Customers.FirstOrDefault(x => x.CustomerID == customerId);
        }
        public static string GetHousingAgencyForCustomer(Customer customer)
        {
            var index = Customers.IndexOf(customer);

            if(index < HousingAgencies.Count)
                return HousingAgencies[index];
            return string.Empty;
        }

        public static List<Customer> GetCustomersForHousingAgency(string housingAgency)
        {
            var index = HousingAgencies.IndexOf(housingAgency);

            return new List<Customer>() { Customers[index] };
        }

        private static Customer BuildCustomer(string firstName, string lastName, int index)
        {
            var customer = new Customer()
            {
                FirstName = firstName,
                LastName = lastName,
                PrimaryEmail = string.Format("{0}.{1}@consilio-soft.com", firstName, lastName)
            };
            GenerateOtherStuff(customer);
            customer.Subscriptions.Insert(0, new Subscription() { Description = HousingAgencies[index], Type = "Housing" });
            return customer;
        }
        private static void GenerateOtherStuff(Customer customer)
        {
            customer.CustomerID = (customer.FirstName + customer.LastName).GetHashCode().ToString();
            customer.Transactions = new List<Transaction>()
                {
                    new Transaction(){BatchNumber = "1111", Description = "Ezpay Aug 2011", Credit = CreditDebit.Debit, Date = new DateTime(2011, 8, 1), Amount = 5000},
                    new Transaction(){BatchNumber = "2222", Description = "Ezpay Seg 2011", Credit = CreditDebit.Debit, Date = new DateTime(2011, 9, 1), Amount = 5000},
                    new Transaction(){BatchNumber = "3333", Description = "Ezpay Refund", Credit = CreditDebit.Credit, Date = new DateTime(2011, 9, 3), Amount = 200}
                };

            customer.Subscriptions = new List<Subscription>()
                {
                    new Subscription() {Description = "Okinawa Gas", Type = "Gas"},
                    new Subscription() {Description = "Okinawa Power", Type = "Electricity"},
                    new Subscription() {Description = "Okinawa City Water", Type = "Water"},
                    new Subscription() {Description = "Okinawa ISP", Type = "ISP"},
                };

            customer.Invoices = new List<Invoice>()
                {
                    new Invoice(){InvoiceNumber = "111111", Date = new DateTime(2011, 8, 1), Amount = 5000},
                    new Invoice(){InvoiceNumber = "222222", Date = new DateTime(2011, 9, 1), Amount = 5000},
                };
        }
    }

    public class Customer
    {
        public string CustomerID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string FullName
        {
            get
            {
                return string.Format("{0} {1}", FirstName, LastName);
            }
        }

        public string PrimaryEmail
        {
            get;
            set;
        }

        public string MiddleInitial { get; set; }
        public string HomePhone { get; set; }
        public string WorkPhone { get; set; }
        public string CellPhone { get; set; }
        public string AlternateEmail { get; set; }
        public string HomeAddress { get; set; }
        public string BuildingName { get; set; }
        public string DutyUnit { get; set; }

        public string BankName { get; set; }
        public string BankAddress { get; set; }
        public string BankRoutingNumber { get; set; }
        public string BankAccountNumber { get; set; }

        public List<Transaction> Transactions { get; set; }
        public List<Subscription> Subscriptions { get; set; }
        public List<Invoice> Invoices { get; set; }

    }

    public class Invoice
    {
        public string InvoiceNumber { get; set; }
        public DateTime Date { get; set; }
        public string DateString
        {
            get
            {
                return Date.ToShortDateString();
            }
        }
        public decimal Amount { get; set; }
    }

    public class Subscription
    {
        public string Description { get; set; }
        public string Type { get; set; }
    }

    public class Transaction
    {
        public string BatchNumber { get; set; }
        public string Description { get; set; }
        public DateTime Date { get; set; }
        public string DateString
        {
            get
            {
                return Date.ToShortDateString();
            }
        }
        public decimal Amount { get; set; }
        public CreditDebit Credit { get; set; }
    }

    public enum CreditDebit
    {
        Credit,
        Debit
    }
}
